from CustomGuiItems.MyDockWidget import MyDockWidget;
from CustomGuiItems.MyTableWidget import MyTableWidget;
from GraphSettings.GraphFeatures.ColorStyle import ColorStyle;
from PyQt4 import QtGui, QtCore;

class GridGrpActsTable(QtCore.QObject):    
    def __init__(self, objData):
        super(GridGrpActsTable, self).__init__();
        self.dataModel = objData.dataModel;
        self.objData = objData;
        self.parent = objData.parent;
        self.mainWin = objData.parent.mainWin;
        self.myDockWidget = MyDockWidget("Group Activities Table", self.parent);
        self.mainWin.addDockWidget(QtCore.Qt.BottomDockWidgetArea, self.myDockWidget);
        self.createDockContent();
        self.myDockWidget.setWidget(self.gridRiskActRes);
        
    []#Update Grid Data from DataModel
    def updateGrid(self):
#         nActs = self.dataModel.nActions;
        dfGrpActs = self.dataModel.dfGrpActs.copy();
        dfGrpActs.insert(0, "Grp-Id", dfGrpActs.index);
        self.gridRiskActRes.setRowCount(len(dfGrpActs));
        self.gridRiskActRes.clearContents();
        for i in range(len(dfGrpActs)):
            for j in range(len(dfGrpActs.columns)):
                qItem = QtGui.QTableWidgetItem(str(dfGrpActs.ix[i][j]));
                if j in [0,1,3,4]:
                    qItem.setTextAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter);
                else:
                    qItem.setTextAlignment(QtCore.Qt.AlignVCenter);
                self.gridRiskActRes.setItem(i,j,qItem);
#         self.gridRiskActRes.setSpan(len(riskActRes)-nActs, 0, nActs, 1);
        self.gridRiskActRes.resizeColumnsToContents();                
        self.gridRiskActRes.resizeRowsToContents();
        
    def onTableEdit(self, row, col):
#        #Format Input
#        nDims = self.dataModel.nDims-1;
#        sStType = ["Normal", "Goal", "Fatal"];
#        sFatal = ["No", "Yes"];
#        iStType = nDims + 3 + self.dataModel.nActions;
#        nRisks = self.dataModel.nRisks
#        iTotalCols = iStType + nRisks;
#        sData = self.gridRiskActRes.item(row,col).data(0).toString();
#        iData = self.gridRiskActRes.item(row,col).data(0).toDouble()[0];
#        iPolicy = nDims+3;
#        if (col>nDims and col!=iPolicy and col<iStType) or col>=iStType+nRisks: #Doubles
#            iData = self.gridRiskActRes.item(row,col).data(0).toDouble()[0];
#        elif col<=nDims: #Integers
#            iData = int(self.gridRiskActRes.item(row,col).data(0).toDouble()[0]);
#        elif col==iStType and sStType.count(sData)==0: #Text StType
#            iData = int(self.gridRiskActRes.item(row,col).data(0).toDouble()[0]);
#            iData = 2 if iData > len(sStType)-1 else iData;
#            tData = sStType[iData];
#        elif col>iStType and col<iTotalCols and sFatal.count(sData)==0: #Text Fatal
#            iData = int(self.gridRiskActRes.item(row,col).data(0).toDouble()[0]);
#            iData = 0 if iData > len(sFatal)-1 else iData;
#            tData = sFatal[iData];
#        bNumber = self.gridRiskActRes.item(row,col).data(0).toDouble()[1];
#        if not bNumber and tData!=sData:
#            self.gridRiskActRes.setItem(row,col,QtGui.QTableWidgetItem(tData));
#            return;
#        if bNumber and sData!=str(iData): #Format the Input Number correctly
#            self.gridRiskActRes.setItem(row,col,QtGui.QTableWidgetItem(str(iData)));
#            return;
        #Fill Data & Refresh Graph
#        fData = self.gridRiskActRes.item(row,col).data(0).toDouble()[0];
#        data = self.gridRiskActRes.item(row,col).data(0);
        pass;
# # #         data = str(self.gridRiskActRes.item(row,col).data(0).toString());
# # #         self.dataModel.riskActRes[row][col] = data;
# # #         self.formatRow(row);
# # #         self.refreshGraph(row, col, data);
# # #         self.gridRiskActRes.item(row,col).setTextAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter);
        
    def refreshGraph(self, row, col, iData):
        sNode = self.dataModel.riskActRes[row][0];
        sAction = self.dataModel.riskActRes[row][1];
        if col == self.dataModel.nRiskActResCols-1: #Action Result
            iNodeId = int(sNode.split(" ")[1]);
            iAct = int(sAction.split(" ")[1])-1;
            graphView = self.objData.graphScene.graphView;
            if iData == "Good Action" or iData == "Normal":
                graphView.setAction_StyleStatus(iNodeId, iAct, 0);
            elif iData == "Risky Action" or iData == "Risky":
                graphView.setAction_StyleStatus(iNodeId, iAct, 2);
        self.objData.graphScene.updateScene();
        
    def formatRow(self, row):
        expValA = float(self.dataModel.riskActRes[row][2]);
        expValB = float(self.dataModel.riskActRes[row][4]);
        maxProbA = float(self.dataModel.riskActRes[row][6]);
        maxProbB = float(self.dataModel.riskActRes[row][8]);
        bExpVal = expValA >= expValB;
        bMaxProb = maxProbA <= maxProbB;
        bResult = bExpVal and bMaxProb;
#        print len(self.dataModel.riskActResBs), row;
        self.dataModel.riskActResBs[row] = [bExpVal, bMaxProb, bResult];
        for i in range(2,5):
            qItem = self.gridRiskActRes.item(row,i);
            if qItem==None: continue;
            if bExpVal: self.colorStyle.setFormatGreen(qItem);
            else: self.colorStyle.setFormatRed(qItem);
        for i in range(6,9):
            qItem = self.gridRiskActRes.item(row,i);
            if qItem==None: continue;
            if bMaxProb: self.colorStyle.setFormatGreen(qItem);
            else: self.colorStyle.setFormatRed(qItem);
        qItem = self.gridRiskActRes.item(row,9);
        if qItem==None: return;
        if bResult: self.colorStyle.setFormatGreen(qItem);
        else: self.colorStyle.setFormatRed(qItem);
        
    def createDockContent(self):
        #Table
        self.gridRiskActRes = MyTableWidget(self.mainWin);
        myItemDelegate = ItemDelegate_StateInfo(self.parent, self.gridRiskActRes);
        self.gridRiskActRes.setItemDelegate(myItemDelegate);
        sCols = ["Grp-Id", "Parent-Id", "Activity", "Start-Frm", "End-Frm", "Role", "Persons"];
        self.gridRiskActRes.setColumnCount(len(sCols));
        self.gridRiskActRes.setRowCount(0);
        self.gridRiskActRes.setHorizontalHeaderLabels(sCols);
        self.gridRiskActRes.connect(self.gridRiskActRes, QtCore.SIGNAL("cellChanged(int,int)"), self.onTableEdit);
        self.colorStyle = ColorStyle();
#         self.updateGrid();
                
    
class ItemDelegate_StateInfo(QtGui.QStyledItemDelegate):
    def __init__(self, parent=None, qTable=None):
        super(ItemDelegate_StateInfo, self).__init__(parent);
        self.qTable = qTable;
    
    def paint(self, painter, option, index):
        super(ItemDelegate_StateInfo, self).paint(painter, option, index);
#        painter.setRenderHint(QtGui.QPainter.Antialiasing);
#        nX = self.qTable.columnViewportPosition(index.column());
#        nY = self.qTable.rowViewportPosition(index.row());
#        nWidth = self.qTable.columnWidth(index.column());
#        nHeight = self.qTable.rowHeight(index.row());
#        if index.column()==1 and index.row()==3:
#            painter.drawEllipse(nX, nY, nWidth, nHeight);
#        elif index.column()==2 and index.row()==3:
#            myPen = QtGui.QPen(QtGui.QColor("Green"));
#            myPen.setWidth(6);
#            painter.setPen(myPen);
#            painter.drawRect(nX, nY, nWidth, nHeight);
